package com.ddwl.schema.serialize;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ddwl.schema.util.DateUtil;

import java.lang.reflect.Type;
import java.time.Instant;

public class InstantSerializer implements ObjectSerializer {

    public final static InstantSerializer instance = new InstantSerializer();


    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeNull(SerializerFeature.WriteDateUseDateFormat);
            return;
        } else {
            out.writeString(DateUtil.formatDateTime((Instant) object));
        }
    }
}